package com.qf.service.impl;

import com.qf.dao.EmplyeeMapper;
import com.qf.dao.jpa.EmplyeeJpa;
import com.qf.entity.Emplyee;
import com.qf.entity.Permission;
import com.qf.entity.Role;
import com.qf.service.EmplyeeService;
import com.qf.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EmplyeeServiceImpl implements EmplyeeService {

    @Resource
    private EmplyeeMapper emplyeeMapper;

    @Resource
    private JavaMailSender javaMailSender;

    @Resource
    private EmplyeeJpa emplyeeJpa;

    @Value("${lance.send}")
    private String mail_send;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public Emplyee login(String ename, String epassword) {
        System.out.println("login:"+ename+epassword);
        return emplyeeMapper.login(ename,epassword);
    }

    @Override
    public List<Emplyee> getAllEmplyee() {
        return emplyeeMapper.getAllEmplyee();
    }

    @Override
    public Emplyee findByEname(String ename) {
        return emplyeeMapper.findByEname(ename);
    }

    @Override
    public List<Permission> getPermissionByUserName(String ename) {
        return emplyeeMapper.getPermissionByUserName(ename);
    }

    @Override
    public Role getByEnameRole(String ename) {
        return emplyeeMapper.getByEnameRole(ename);
    }

    @Override
    public Integer getByEnameEmplyee(String ename) {
        return emplyeeMapper.getByEnameEmplyee(ename);
    }

    @Override
    public void updateRole(Integer eid, Integer rid) {
        emplyeeMapper.updateRole(eid,rid);
    }

    @Override
    public int deleteEmplyee(String ename) {
        return emplyeeMapper.deleteEmplyee(ename);
    }

    @Override
    public void updEmplyee(Emplyee emplyee) {
        emplyeeMapper.updEmplyee(emplyee);
    }

    @Override
    public Emplyee saveOne(Emplyee emp) {
        return emplyeeJpa.saveAndFlush(emp);
    }

    @Override
    public String sendCode(String email) {
        double x = Math.random()*1000000;
        int y = (int) x;
        String code = Integer.toString(y);

        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(mail_send);
            simpleMailMessage.setText(code);
            simpleMailMessage.setSubject("giao的验证码");
            simpleMailMessage.setTo(email);
            javaMailSender.send(simpleMailMessage);
            redisUtil.set(email,code);
            redisUtil.expire(email,10000);
            return "success";
        } catch (MailException e) {
            e.printStackTrace();
            return "defeat";
        }


    }

    @Override
    public Emplyee findByEmail(String eemail) {
        return emplyeeMapper.findByEmail(eemail);
    }

    @Override
    public String getEnameByTel(String etel) {
        return emplyeeMapper.getEnameByTel(etel);
    }
}
